﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Zebra.DataAccess;

namespace Zebra
{
    public partial class UserFormold : Form
    {
        User user;
        public List<User> Users { get; set; }

        public UserFormold()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            InitializePage();
        }

        private void InitializePage()
        {
            user = User.Create();
            dataGridView1.AutoGenerateColumns = false;
            Users = Repository.UserRespository(false).GetAllUsers();
            UserBindingSource.DataSource = Users;
        }

        private void saveValueToObject()
        {
            user.Name = txtName.Text;
            user.Address = txtAddress.Text;
            user.Dob = DateTime.Now;

            Staff staff = new Staff();
            staff.Password = txtPassword.Text;
            staff.DateJoined = DateTime.Now;
            staff.DateLeft = DateTime.Now;

            user.Staffs.Add(staff);
        }

        private void Clear()
        {
            txtName.Text = string.Empty;
            txtAddress.Text = string.Empty;
            txtPassword.Text = string.Empty;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            saveValueToObject();
            if (user.Validate())
            {
                if (user.Id == 0)
                {
                    Repository.UserRespository(false).Insert(user);
                    Users.Add(user);
                }
                else
                {
                    Repository.UserRespository(false).Save();
                }
                UserBindingSource.ResetBindings(false);
            }
            else
            {
                errorProvider1.UpdateBinding();
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (user != null)
            {
                Users.Remove(user);
                Repository.UserRespository(false).Delete(user);
                UserBindingSource.ResetBindings(false);
            }
        }
        private void btnNew_Click(object sender, EventArgs e)
        {
            user = User.Create();
            Clear();

        }
        private void btnReset_Click(object sender, EventArgs e)
        {
            Repository.UserRespository(false).Refresh(user);
        }     

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            user = (User)dataGridView1.CurrentRow.DataBoundItem;

            if (user == null) return;
            txtName.Text = user.Name;
            txtAddress.Text = user.Address;
            txtPassword.Text = user.Staffs[0].Password;
        }

        //public void PropertyChanged(object sender, PropertyChangedEventArgs e)
        //{
        //    switch (e.PropertyName)
        //    {
        //        case "Name":
        //            user.IsNameValid();
        //            break;
        //    }
        //}

       
      
    }
}
